﻿var ACVT_Chat_Id=0;

var accountUser=new Object();
//var login_url="http://192.168.0.10/phpInterface/login_php.txt";
var login_url;

function AddAccountUser(client,username,psw,copId){
	if(accountUser[copId]==undefined){
		accountUser[copId]=new Object();
	}
	if(accountUser[copId][username]!=null){
		application.rejectConnection(client,{code:101,error:"can't multi-login!"});
	}else{
		//here need PHP
		client.username=username;
		client.copId=copId;
		
		var loginLv=new LoadVars();
		
		
		loginLv.root=this;
		loginLv.client=client;
		loginLv.onLoad=onCheckLogin;
		loginLv.onHTTPStatus = function(httpStatus)
		{
			trace("["+loginLv.contentType+"]HTTPStatus >>>>> "+httpStatus);
		}
		
		
		/*
		loginLv.onData=function(src){
			this.root.writeLog(src.toString());
		}
		loginLv.username=username;
		loginLv.psw=psw;
		loginLv.copId=copId;
		*/
		login_url=configs.loginInterface;
		login_url+="?username="+username+"&psw="+psw+"&copId="+copId+"&action=login&__r="+Math.floor(Math.random()*1000).toString();
		trace("* checkLOGIN:"+login_url);

		loginLv.load(login_url);
		//trace("--------"+client.copId);
		
		//MyTrace(loginLv);
	}
	
}
function onCheckLogin(success){
	var root=this.root;
	root.writeLog(this.toString());
	trace("check-login-action - "+success+" RS:"+(this.login=="1"));
	trace(this.uid+"|"+this.username+"|"+this.copId+"|"+this.nickname+"|"+this.client.username+"|"+this.client.copId);
	if(success){
		if(this.login=="1"){
			if(root.accountUser[this.copId]==undefined){
				root.accountUser[this.copId]={};
			}
			if(root.accountUser[this.copId][this.username]!=null){
				application.rejectConnection(this.client,{code:101,error:"can't multi-login!"});
				return;
			}
			this.client.TakeAnAskOfVister=root.TakeAnAskOfVister;
			this.client.uid=this.uid;
			root.accountUser[this.copId][this.username]={
				client:this.client,
				username:this.username,
				uid:this.uid,
				psw:this.psw,
				nickname:this.nickname
				};
			application.acceptConnection(this.client);
			root.CallClient(this.client,"InitUserInfos",{
				username:this.username,
				companyid:this.copId,
				nickname:this.nickname
				});
			root.CallClient(this.client,"VisterAskingList",root.GetVisterNameList(this.copId));
		}else{
			application.rejectConnection(this.client,{code:102,error:"UserName OR PSW Error."});
		}
	}else{
		application.rejectConnection(this.client,{code:501,error:"server error:Login in check Error."});
	}
}
function DelAccountUser(client){
	if(accountUser[client.copId][client.username]!=null){
		var loginLv=new LoadVars();
		var login_url=configs.loginInterface;
		
		login_url+="?uid="+client.uid+"&copId="+client.copId+"&action=logout&__r="+Math.floor(Math.random()*1000).toString();
		trace("* checkLOGOUT:"+login_url);
		loginLv.load(login_url);
		
		delete accountUser[client.copId][client.username];
		trace("Delete ["+client.username+"] from account list");
	}else{
		trace("Delete client not find....");
	}
}
function AC_BroadcastCopMsg(copId,FunName,msg){
	for(var au in accountUser[copId]){
		var c = accountUser[copId][au].client;
		//trace("call-"+FunName+"|"+msg);
		c.call(FunName,null,msg);
	}
}

function AC_BroadcastMsg(FunName,msg){
	for(var au in accountUser){
		var cc = accountUser[au];
		for(var ac in cc){
			var c=cc[ac].client;
			c.call(FunName,null,msg);
		}
	}
}

//take an chart ask 
function TakeAnAskOfVister(vId){
	var root=this.app;
	//trace(root+"||"+this+"|||"+this.app);
	//trace(GetVister+"|"+GetLessLoadingCartServer+"|"+this.clientType);
	var cartServer=GetLessLoadingCartServer();
	if(cartServer==undefined){
		//no cartServer availab
		return {RS:false,msg:"No CartServerAvailab",id:vId};
	}
	var client=GetVister(this.copId,vId);
	if(client==undefined|| client==null){
		//Take faile
		return {RS:false,msg:"No VisterFind",id:vId};
	}
	else{
		//find vister and cart server!!
		client.tacked=true;
		ACVT_Chat_Id++;
		//trace("Start Cart process G["+ACVT_Chat_Id+"]"+(ACVT_Chat_Id+1));
		CallClient(client.client,"AskingBeTaked",
		{
			ac_id:client.ID,
			msg:"Start Cart process",
			chatid:ACVT_Chat_Id,
			uri:cartServer.uri
			});
		
		return {
			RS:true,
			msg:"Start Cart process",
			id:vId,
			chatid:ACVT_Chat_Id,
			uri:cartServer.uri
			};
	}
}

